import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

const store = new Vuex.Store({
  //定义全局属性
  state: {
    token: '',
    userInfo: {}
  },
  //修改state属性的唯一方法是提交mutation
  mutations: {
    setToken(state, token){
      // localStorage.setItem('token', state.token);  //??此处为什么是state.token
      // this.state.token = token;
      state.token = token;
    },

    setUserInfo(state, data){
      // data = JSON.stringify(data);
      // localStorage.setItem('userInfo',data);
      // this.state.userInfo = data;
      state.userInfo = data;
    }

  },
  //计算属性
  getters: {
    getToken: (state) => {
      // state.token = localStorage.getItem('token');
      return state.token;
    },
    getUserInfo: state => {
      // state.userInfo = localStorage.getItem('userInfo');
      // if(state.userInfo){
      //   return JSON.parse(state.userInfo);
      // }else{
      //   return state.userInfo;
      // }
      return state.userInfo;
    }
  }
});

store.getToken = () => {
  return store.state.token;
}

export default store;
